package billing.utils.p;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class NetUtils
{

	/**
	 * 下载HTTP资源另存为文件
	 * 
	 * @param destUrl
	 *        String
	 * @param fileName
	 *        String
	 * @throws Exception
	 */
	public static boolean downloadFile(String destUrl, String fileName)
	{
		String remoteDestUrl = destUrl.trim().replaceAll(" ", "%20");
		File destFile = new File(fileName);
		if (destFile.exists())
		{
			destFile.delete();
		}
		else
		{
			File dir = destFile.getParentFile();
			if (!dir.exists())
			{
				dir.mkdirs();
			}
		}

		FileOutputStream fos = null;
		BufferedInputStream bis = null;
		HttpURLConnection httpUrl = null;
		byte[] buf = new byte[8096];
		int size = 0;
		// 建立链接
		try
		{
			URL url = new URL(remoteDestUrl);
			httpUrl = (HttpURLConnection) url.openConnection();
			httpUrl.setReadTimeout(300000);
			httpUrl.setConnectTimeout(300000);
			// 连接指定的资源
			httpUrl.connect();
			// 获取网络输入流
			bis = new BufferedInputStream(httpUrl.getInputStream());

			// 建立文件
			fos = new FileOutputStream(fileName);
			while ((size = bis.read(buf)) != -1)
			{
				fos.write(buf, 0, size);
			}

			return true;
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if (fos != null)
			{
				try
				{
					fos.close();
				}
				catch (IOException e)
				{
				}
			}
			if (bis != null)
			{
				try
				{
					bis.close();
				}
				catch (IOException e)
				{
				}
			}
			if (httpUrl != null)
				httpUrl.disconnect();
		}
		return false;
	}

	/**
	 * 读取远程的HTTP资源
	 * 
	 * @param destUrl
	 * @return
	 */
	public static String readRemotePage(String destUrl)
	{
		return readRemotePage(destUrl, null, null);
	}

	/**
	 * 读取远程的HTTP资源
	 * 
	 * @param destUrl
	 * @param method
	 * @param charset
	 * @return
	 */
	public static String readRemotePage(String destUrl, String method, String charset)
	{
		StringBuffer retVal = null;
		BufferedReader reader = null;
		HttpURLConnection conn = null;
		try
		{
			URL url = new URL(destUrl);
			conn = (HttpURLConnection) url.openConnection();
			if (charset != null)
				conn.setRequestProperty("Accept-Charset", charset);
			if (method != null)
				conn.setRequestMethod(method);
			conn.setReadTimeout(300000);
			conn.setConnectTimeout(300000);
			// 连接指定的资源
			conn.connect();
			int code = conn.getResponseCode();
			if (code == 200)
			{
				int ts = conn.getContentLength();

				retVal = new StringBuffer(ts < 0 ? 0 : ts);

				// 获取网络输入流
				reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
				String line = null;
				while ((line = reader.readLine()) != null)
				{
					retVal.append(line).append("\n");
				}
				return retVal.toString();
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if (reader != null)
			{
				try
				{
					reader.close();
				}
				catch (IOException e)
				{
				}
			}
			if (conn != null)
				conn.disconnect();
		}
		return null;
	}
}
